# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def breadcrumb_for_category(category)
    breadcrumb = "<a href='/'>首页</a>"
    if category
      brand = Brand.find(params[:brand_id]) if params[:brand_id]
      category.ancestors().reverse!.each do |c|
        if params[:brand_id]
          if c.parent
            breadcrumb += "><a href='/categories/#{c.id.to_s}?brand_id=#{params[:brand_id]}'>#{c.title}</a>"
          else
            breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.title}</a>"
          end
        else
          breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.title}</a>"
        end

        if !c.parent_id && params[:brand_id]
          breadcrumb += "><a href='/categories/#{c.id.to_s}?brand_id=#{params[:brand_id]}'>#{brand.name}</a>"
        end
      end

      if params[:brand_id] && !category.parent_id
        breadcrumb += "><a href='/categories/#{category.id.to_s}'>#{category.title}</a>>#{brand.name}"
      else
        breadcrumb += ">#{category.title}"
      end
    end

    return breadcrumb
  end

  def breadcrumb_for_product(product)
    breadcrumb = "<a href='/'>首页</a>"

    if product
      product.category.ancestors().reverse.each do |c|
        breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.title}</a>"
      end
      breadcrumb += "><a href='/categories/#{product.category.id.to_s}'>#{product.category.title}</a>>#{product.name}"
    end

    return breadcrumb
  end

  def is_admin(user_id)
    user = user_id &&  User.find(user_id)
    if user
      user.has_role?('administrator')
    else
      false
    end
  end

  def use_tinymce
    @content_for_tinymce = ""
    content_for :tinymce do
      javascript_include_tag "tiny_mce/tiny_mce"
    end
    @content_for_tinymce_init = ""
    content_for :tinymce_init do
      javascript_include_tag "mce_editor"
    end
  end


  def format_status(status)
    (Order::STATUS.find {|s| s[1] == status})[0]
  end

  def format_shipping_type(shipping_type)
    (Order::SHIPPING_TYPE.find {|s| s[1] == shipping_type})[0]
  end

  def format_day(day)
    (Address::DAY.find {|s| s[1] == day})[0]
  end

  def format_timing(timing)
    (Address::TIMING.find {|s| s[1] == timing})[0]
  end



  def format_boolean(value)
    value ? "<img src='/images/tick.png' alt='true'></img>":"<img src='/images/cross.png' alt='false'></img>"
  end

  def lastest_orders
    return_string = ""
    orders = Order.find(:all,:limit=>10,:order=>'id desc')
    orders.each do |o|
      return_string += "<li>#{o.user.login}的订单:#{format_status o.status}</li>"
    end
    return_string
  end

  def back_url
    request.env["HTTP_REFERER"] || '/store'
  end

  
 
  def nav_categories(categories = Category.parent_categories,nav_html = " ")
    #    if cache_exist?('cate_nav')
    #      return
    #    else
    if nav_html.blank?
      ret = "<ul class='sf-menu sf-vertical'><li><a href='/store'>首页</a></li>"
    else
      ret = "<ul>"
    end
    
    for category in categories
      ret << display_category(category)
    end
    ret << "</ul>"
    #    end
  end
  
  def brand_list
    ret = "<dd id='brand_list'>"
    Brand.find(:all,:limit=>15).each do |b|
      ret << "<a href='/brands/#{b.id}'>#{b.name}<span>|</span></a>"
    end
    ret << "</dd>"
  end

  private

  def display_category(category)
    nav_html = "<li>"
    category_name = category.title
    nav_html << "<a href='/categories/#{category.id}'>#{category_name}</a>"
    nav_html << nav_categories(category.children,nav_html) if category.children.size > 0
    nav_html << "</li>"
  end

end
